/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import Conexao.ConsultaBD;
import Entidade.Agendamento;
import Entidade.AgendamentoEmail;
import Entidade.AgendamentoEquipamento;
import Entidade.Equipamento;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author francis.almeida
 */
public class excAgendamentoServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        RequestDispatcher rd;
        try {
            
            if(!request.getParameter("id_agendamento").isEmpty())
            {
                String autenticador = request.getParameter("autenticador");
                
                if(!autenticador.isEmpty())
                {
                
                Object agendamentoObj = ConsultaBD.SelecionarFrom("Agendamento where id=" + request.getParameter("id_agendamento"));
                
                if(((Agendamento)agendamentoObj).getAutenticador().equals(autenticador)){
                    
               try {
                   
                List<Object> listEmail = ConsultaBD.ListaFrom("AgendamentoEmail where agendamento = " + request.getParameter("id_agendamento"));  
                
                if(!listEmail.isEmpty())
                {
                    for(Object o : listEmail){
                      ((AgendamentoEmail)o).Deletar(((AgendamentoEmail)o));
                    }
                }
                
                List<Object> listEquipamentos = ConsultaBD.ListaFrom("AgendamentoEquipamento where agendamento = " + request.getParameter("id_agendamento"));
                
                if(!listEquipamentos.isEmpty())
                {
                    for(Object o : listEquipamentos)
                    {
                        ((AgendamentoEquipamento)o).Deletar((AgendamentoEquipamento)o);
                    }
                }
                
                ((Agendamento)agendamentoObj).Deletar((Agendamento)agendamentoObj);
                
                
                rd = request.getRequestDispatcher("index.jsp");
                rd.include(request, response);
                String erro = "<script>"
                        + "alert('Excluido com sucesso!');"
                        + "</script>";
                        
                 out.print(erro);
                 

                } catch (Exception e){
                    
                     String erro = "<script>"
                        + "alert('Erro: Não pe possível excluir! -> " + e.getMessage() + "');"
                        + "</script>";
                        
                    out.print(erro);
                    rd = request.getRequestDispatcher("agendamentos.jsp");
                    rd.include(request, response);
                    
                    
                }
                
                }
                else{

                     String erro = "<script>"
                        + "alert('Erro: Código de autenticação incorreto');"
                        + "</script>";
                     
                     out.print(erro);
                     rd = request.getRequestDispatcher("agendamentos.jsp");
                     rd.include(request, response);
                        
                }
                
                }
                else{
                    
                     String erro = "<script>"
                        + "alert('Erro: Erro com o código de autenticação ');"
                        + "</script>";
                     
                      out.print(erro);
                     rd = request.getRequestDispatcher("agendamentos.jsp");
                     rd.include(request, response);
                        
                }
                
            }
            
            
        } finally {   
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
